//
// Description: 5896. 股票价格波动
// Created by Loading on 2021/10/10.
//

#include <bits/stdc++.h>

using namespace std;

class StockPrice {
public:
    StockPrice() = default;

    void update(int timestamp, int price) {
        // 当前时间戳的价格存在
        if (m.find(timestamp) != m.end()) {
            if (_price[m[timestamp]] > 0) {
                // 此价格的次数 - 1
                _price[m[timestamp]]--;
                // 若移除以后次数为 0，映射中直接删除
                if (_price[m[timestamp]] <= 0) {
                    _price.erase(m[timestamp]);
                }
            }
        }
        // 更新两个映射
        _price[price]++;
        m[timestamp] = price;
    }

    int current() {
        return (--m.end())->second;
    }

    int maximum() {
        return (--_price.end())->first;
    }

    int minimum() {
        return (_price.begin())->first;
    }

private:
    // <时间戳, 价格> 映射
    map<int, int> m;
    // <价格, 出现次数> 映射
    map<int, int> _price;
};

int main() {
    auto *obj = new StockPrice();
    obj->update(1, 10);
    obj->update(2, 5);
    cout << obj->current() << endl;
    cout << obj->maximum() << endl;
    obj->update(1, 3);
    cout << obj->maximum() << endl;
    obj->update(4, 2);
    cout << obj->minimum() << endl;

    return 0;
}